package b.p.b;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.view.MotionEvent;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.l;
import androidx.recyclerview.widget.RecyclerView.p;
import androidx.recyclerview.widget.RecyclerView.q;
import androidx.recyclerview.widget.RecyclerView.w;
import com.huawei.agconnect.exception.AGCServerException;
import com.pgl.sys.ces.out.ISdkLite;
import java.util.List;
import java.util.WeakHashMap;

/* compiled from: FastScroller */
public class o extends l implements p {
    public static final int[] D = new int[]{16842919};
    public static final int[] E = new int[0];
    public int A;
    public final Runnable B;
    public final q C;
    public final int a;
    public final int b;
    public final StateListDrawable c;
    public final Drawable d;
    public final int e;
    public final int f;
    public final StateListDrawable g;
    public final Drawable h;
    public final int i;
    public final int j;
    public int k;
    public int l;
    public float m;
    public int n;
    public int o;
    public float p;
    public int q = 0;
    public int r = 0;
    public RecyclerView s;
    public boolean t = false;
    public boolean u = false;
    public int v = 0;
    public int w = 0;
    public final int[] x = new int[2];
    public final int[] y = new int[2];
    public final ValueAnimator z;

    /* compiled from: FastScroller */
    public class a implements Runnable {
        public void run() {
            o oVar = o.this;
            int i = oVar.A;
            if (i == 1) {
                oVar.z.cancel();
            } else if (i != 2) {
                return;
            }
            oVar.A = 3;
            oVar.z.setFloatValues(new float[]{((Float) oVar.z.getAnimatedValue()).floatValue(), 0.0f});
            oVar.z.setDuration((long) AGCServerException.UNKNOW_EXCEPTION);
            oVar.z.start();
        }
    }

    /* compiled from: FastScroller */
    public class c extends AnimatorListenerAdapter {
        public boolean a = false;

        public void onAnimationCancel(Animator animator) {
            this.a = true;
        }

        public void onAnimationEnd(Animator animator) {
            if (this.a) {
                this.a = false;
                return;
            }
            o oVar;
            if (((Float) o.this.z.getAnimatedValue()).floatValue() == 0.0f) {
                oVar = o.this;
                oVar.A = 0;
                oVar.m(0);
            } else {
                oVar = o.this;
                oVar.A = 2;
                oVar.s.invalidate();
            }
        }
    }

    /* compiled from: FastScroller */
    public class d implements AnimatorUpdateListener {
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            int floatValue = (int) (((Float) valueAnimator.getAnimatedValue()).floatValue() * 255.0f);
            o.this.c.setAlpha(floatValue);
            o.this.d.setAlpha(floatValue);
            o.this.s.invalidate();
        }
    }

    /* compiled from: FastScroller */
    public class b extends q {
        public void b(RecyclerView recyclerView, int i, int i2) {
            o oVar = o.this;
            i2 = recyclerView.computeHorizontalScrollOffset();
            int computeVerticalScrollOffset = recyclerView.computeVerticalScrollOffset();
            int computeVerticalScrollRange = oVar.s.computeVerticalScrollRange();
            int i3 = oVar.r;
            boolean z = computeVerticalScrollRange - i3 > 0 && i3 >= oVar.a;
            oVar.t = z;
            int computeHorizontalScrollRange = oVar.s.computeHorizontalScrollRange();
            int i4 = oVar.q;
            boolean z2 = computeHorizontalScrollRange - i4 > 0 && i4 >= oVar.a;
            oVar.u = z2;
            boolean z3 = oVar.t;
            if (z3 || z2) {
                if (z3) {
                    float f = (float) i3;
                    oVar.l = (int) ((((f / 2.0f) + ((float) computeVerticalScrollOffset)) * f) / ((float) computeVerticalScrollRange));
                    oVar.k = Math.min(i3, (i3 * i3) / computeVerticalScrollRange);
                }
                if (oVar.u) {
                    float f2 = (float) i2;
                    float f3 = (float) i4;
                    oVar.o = (int) ((((f3 / 2.0f) + f2) * f3) / ((float) computeHorizontalScrollRange));
                    oVar.n = Math.min(i4, (i4 * i4) / computeHorizontalScrollRange);
                }
                computeVerticalScrollOffset = oVar.v;
                if (computeVerticalScrollOffset == 0 || computeVerticalScrollOffset == 1) {
                    oVar.m(1);
                }
            } else if (oVar.v != 0) {
                oVar.m(0);
            }
        }
    }

    public o(RecyclerView recyclerView, StateListDrawable stateListDrawable, Drawable drawable, StateListDrawable stateListDrawable2, Drawable drawable2, int i, int i2, int i3) {
        ValueAnimator ofFloat = ValueAnimator.ofFloat(new float[]{0.0f, 1.0f});
        this.z = ofFloat;
        this.A = 0;
        this.B = new a();
        b bVar = new b();
        this.C = bVar;
        this.c = stateListDrawable;
        this.d = drawable;
        this.g = stateListDrawable2;
        this.h = drawable2;
        this.e = Math.max(i, stateListDrawable.getIntrinsicWidth());
        this.f = Math.max(i, drawable.getIntrinsicWidth());
        this.i = Math.max(i, stateListDrawable2.getIntrinsicWidth());
        this.j = Math.max(i, drawable2.getIntrinsicWidth());
        this.a = i2;
        this.b = i3;
        stateListDrawable.setAlpha(ISdkLite.REGION_UNSET);
        drawable.setAlpha(ISdkLite.REGION_UNSET);
        ofFloat.addListener(new c());
        ofFloat.addUpdateListener(new d());
        RecyclerView recyclerView2 = this.s;
        if (recyclerView2 != recyclerView) {
            if (recyclerView2 != null) {
                recyclerView2.f0(this);
                recyclerView2 = this.s;
                recyclerView2.p.remove(this);
                if (recyclerView2.q == this) {
                    recyclerView2.q = null;
                }
                List list = this.s.k0;
                if (list != null) {
                    list.remove(bVar);
                }
                i();
            }
            this.s = recyclerView;
            recyclerView.g(this);
            this.s.p.add(this);
            this.s.h(bVar);
        }
    }

    public boolean a(RecyclerView recyclerView, MotionEvent motionEvent) {
        int i = this.v;
        if (i == 1) {
            boolean k = k(motionEvent.getX(), motionEvent.getY());
            boolean j = j(motionEvent.getX(), motionEvent.getY());
            if (motionEvent.getAction() == 0 && (k || j)) {
                if (j) {
                    this.w = 1;
                    this.p = (float) ((int) motionEvent.getX());
                } else if (k) {
                    this.w = 2;
                    this.m = (float) ((int) motionEvent.getY());
                }
                m(2);
                return true;
            }
        } else if (i == 2) {
            return true;
        }
        return false;
    }

    public void c(RecyclerView recyclerView, MotionEvent motionEvent) {
        if (this.v != 0) {
            if (motionEvent.getAction() == 0) {
                boolean k = k(motionEvent.getX(), motionEvent.getY());
                boolean j = j(motionEvent.getX(), motionEvent.getY());
                if (k || j) {
                    if (j) {
                        this.w = 1;
                        this.p = (float) ((int) motionEvent.getX());
                    } else if (k) {
                        this.w = 2;
                        this.m = (float) ((int) motionEvent.getY());
                    }
                    m(2);
                }
            } else if (motionEvent.getAction() == 1 && this.v == 2) {
                this.m = 0.0f;
                this.p = 0.0f;
                m(1);
                this.w = 0;
            } else if (motionEvent.getAction() == 2 && this.v == 2) {
                float x;
                int[] iArr;
                n();
                if (this.w == 1) {
                    x = motionEvent.getX();
                    iArr = this.y;
                    int i = this.b;
                    iArr[0] = i;
                    iArr[1] = this.q - i;
                    x = Math.max((float) iArr[0], Math.min((float) iArr[1], x));
                    if (Math.abs(((float) this.o) - x) >= 2.0f) {
                        i = l(this.p, x, iArr, this.s.computeHorizontalScrollRange(), this.s.computeHorizontalScrollOffset(), this.q);
                        if (i != 0) {
                            this.s.scrollBy(i, 0);
                        }
                        this.p = x;
                    }
                }
                if (this.w == 2) {
                    x = motionEvent.getY();
                    iArr = this.x;
                    int i2 = this.b;
                    iArr[0] = i2;
                    iArr[1] = this.r - i2;
                    x = Math.max((float) iArr[0], Math.min((float) iArr[1], x));
                    if (Math.abs(((float) this.l) - x) >= 2.0f) {
                        i2 = l(this.m, x, iArr, this.s.computeVerticalScrollRange(), this.s.computeVerticalScrollOffset(), this.r);
                        if (i2 != 0) {
                            this.s.scrollBy(0, i2);
                        }
                        this.m = x;
                    }
                }
            }
        }
    }

    public void e(boolean z) {
    }

    public void h(Canvas canvas, RecyclerView recyclerView, w wVar) {
        if (this.q == this.s.getWidth() && this.r == this.s.getHeight()) {
            if (this.A != 0) {
                int i;
                int i2;
                int i3;
                int i4;
                if (this.t) {
                    i = this.q;
                    i2 = this.e;
                    i -= i2;
                    i3 = this.l;
                    i4 = this.k;
                    i3 -= i4 / 2;
                    this.c.setBounds(0, 0, i2, i4);
                    this.d.setBounds(0, 0, this.f, this.r);
                    RecyclerView recyclerView2 = this.s;
                    WeakHashMap weakHashMap = b.i.k.o.a;
                    Object obj = 1;
                    if (recyclerView2.getLayoutDirection() != 1) {
                        obj = null;
                    }
                    if (obj != null) {
                        this.d.draw(canvas);
                        canvas.translate((float) this.e, (float) i3);
                        canvas.scale(-1.0f, 1.0f);
                        this.c.draw(canvas);
                        canvas.scale(1.0f, 1.0f);
                        canvas.translate((float) (-this.e), (float) (-i3));
                    } else {
                        canvas.translate((float) i, 0.0f);
                        this.d.draw(canvas);
                        canvas.translate(0.0f, (float) i3);
                        this.c.draw(canvas);
                        canvas.translate((float) (-i), (float) (-i3));
                    }
                }
                if (this.u) {
                    i = this.r;
                    i2 = this.i;
                    i -= i2;
                    i3 = this.o;
                    i4 = this.n;
                    i3 -= i4 / 2;
                    this.g.setBounds(0, 0, i4, i2);
                    this.h.setBounds(0, 0, this.q, this.j);
                    canvas.translate(0.0f, (float) i);
                    this.h.draw(canvas);
                    canvas.translate((float) i3, 0.0f);
                    this.g.draw(canvas);
                    canvas.translate((float) (-i3), (float) (-i));
                }
            }
            return;
        }
        this.q = this.s.getWidth();
        this.r = this.s.getHeight();
        m(0);
    }

    public final void i() {
        this.s.removeCallbacks(this.B);
    }

    public boolean j(float f, float f2) {
        if (f2 >= ((float) (this.r - this.i))) {
            int i = this.o;
            int i2 = this.n;
            if (f >= ((float) (i - (i2 / 2))) && f <= ((float) ((i2 / 2) + i))) {
                return true;
            }
        }
        return false;
    }

    public boolean k(float f, float f2) {
        RecyclerView recyclerView = this.s;
        WeakHashMap weakHashMap = b.i.k.o.a;
        if ((recyclerView.getLayoutDirection() == 1 ? 1 : null) != null) {
            if (f > ((float) (this.e / 2))) {
                return false;
            }
        } else if (f < ((float) (this.q - this.e))) {
            return false;
        }
        int i = this.l;
        int i2 = this.k / 2;
        if (f2 < ((float) (i - i2)) || f2 > ((float) (i2 + i))) {
            return false;
        }
        return true;
    }

    public final int l(float f, float f2, int[] iArr, int i, int i2, int i3) {
        int i4 = iArr[1] - iArr[0];
        if (i4 == 0) {
            return 0;
        }
        i -= i3;
        int i5 = (int) (((f2 - f) / ((float) i4)) * ((float) i));
        i2 += i5;
        return (i2 >= i || i2 < 0) ? 0 : i5;
    }

    public void m(int i) {
        if (i == 2 && this.v != 2) {
            this.c.setState(D);
            i();
        }
        if (i == 0) {
            this.s.invalidate();
        } else {
            n();
        }
        if (this.v == 2 && i != 2) {
            this.c.setState(E);
            i();
            this.s.postDelayed(this.B, (long) 1200);
        } else if (i == 1) {
            i();
            this.s.postDelayed(this.B, (long) 1500);
        }
        this.v = i;
    }

    public void n() {
        int i = this.A;
        if (i != 0) {
            if (i == 3) {
                this.z.cancel();
            } else {
                return;
            }
        }
        this.A = 1;
        this.z.setFloatValues(new float[]{((Float) this.z.getAnimatedValue()).floatValue(), 1.0f});
        this.z.setDuration(500);
        this.z.setStartDelay(0);
        this.z.start();
    }
}
